package com.gxa.service.impl;

import com.gxa.dto.ResultDto;
import com.gxa.exception.SystemException;
import com.gxa.group.LoginName;
import com.gxa.mapper.UserMapper;
import com.gxa.pojo.User;
import com.gxa.service.UserService;
import com.gxa.util.MD5Util;
import com.gxa.util.Response;
import com.gxa.validator.MyValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
/**
 * @author 王康
 */
@Service("nameService")
public class UserServiseImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MyValidator validator;

    @Override
    public ResultDto login(User user, HttpSession session) {
    validator.validate(user, LoginName.class);
        User dbUser = userMapper.findByName(user.getUserName());
        if (dbUser == null) {
            throw new SystemException(1003,"该用户不存在");
        }
        if (!dbUser.getUserPassword().equals(MD5Util.MD55(user.getUserPassword()))) {
            throw new SystemException(1004,"111");
        }
        session.setAttribute("userName",dbUser.getUserName());
        return Response.success(200,"登录成功");
    }
}
